/**
 * @author of2198
 * @date 2017/4/26.
 * @description
 */

import { Store } from "iflux2";
import TemplateActor from "./actor/template-actor";
import initOpenApi from "../common/initOpenApi";

declare let xDllContext: any;

export default class AppStore extends Store {
  bindActor() {
    return [new TemplateActor()];
  }

  //获取模板列表
  getTemplateList = async ({ platform, includePageTypeList }) => {
    let openApi = await initOpenApi();
    return openApi.TplService
      .getTemplateList({
        platform,
        pageSize: -1,
        includePageTypeList,
        isShow: true
      })
      .then(
        result => {
          this.dispatch("SET_TEMPLATE_LIST", {
            data: result,
            platform
          });
        },
        err => {
          return Promise.reject(err);
        }
      );
  };

  //获取平台类型的btn列表
  getPlatformBtnList = async () => {
    let openApi = await initOpenApi();
    return openApi.SysDicService.getPlatformTabInfo().then(
      result => {
        this.dispatch("SET_PLATFORM_BTN_LIST", result);
        return Promise.resolve(result);
      },
      err => {
        return Promise.reject(err);
      }
    );
  };

  //获取页面类型的btn列表
  getPageTypeBtnList = async ({ platform }) => {
    let openApi = await initOpenApi();
    return openApi.SysDicService.getPageTypeTabInfo(platform).then(
      result => {
        this.dispatch("SET_PAGE_TYPE_BTN_LIST", result);
      },
      err => {
        return Promise.reject(err);
      }
    );
  };

  //获取用户的每种页面类型的数量
  getPageTypeCount = async ({ platform, pageType }) => {
    let openApi = await initOpenApi();
    return openApi.PageService
      .getPageInfoCount({
        platform,
        includePageTypeList: [pageType]
      })
      .then(
        result => {
          this.dispatch("SET_PAGE_TYPE_COUNT", result);
        },
        err => {
          return Promise.reject(err);
        }
      );
  };

  //获取用户激活首页信息
  getActivatedPageInfo = async platform => {
    let openApi = await initOpenApi();
    return openApi.PageService.getActivatedPageInfo(platform).then(
      result => {
        this.dispatch("SET_ACTIVATED_PAGE_INFO", result);
      },
      err => {
        return Promise.reject(err);
      }
    );
  };
}
